package advancedMobs.entity.advancedMob;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import advancedMobs.entity.AI.EntityAIMoveTowardsLeader;
import net.minecraft.entity.Entity;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.monster.EntityMob;
import net.minecraft.world.World;

public abstract class AdvancedMob extends EntityMob 
{
    protected static List<AdvancedMob> horde;
    protected static int leaderID = -1;
    protected static Entity goal;
    protected boolean leader = false;
    public EntityItem targetBlock;
    
	public AdvancedMob(World par1World) 
	{
		super(par1World);
		cleanHorde();
	}
	
	public abstract int getMaxHealth();
	
	public void findGoal()
	{
		Iterator entities = worldObj.loadedEntityList.iterator();
		while(entities.hasNext())
		{
			Entity entity = (Entity) entities.next();
			
			if(entity.entityId == 56)
			{
				System.out.println("Found Diamond Block");
				goal = entity;
			}
		}
	}
	
	public AdvancedMob returnLeader()
	{
		AdvancedMob leader = (AdvancedMob) this.worldObj.getEntityByID(leaderID);
		//System.out.println(horde.size());
		if(leaderID == -1 || leader == null)
		{
			int i=0;
			for(;i<horde.size();i++)
			{
				if(horde.get(i) == null)
				{
					System.out.println("Removed" + i);
					horde.remove(i);
				}
				else
				{
					break;
				}
			}
			leader = horde.get(i);
			leaderID = leader.entityId;
			leader.setLeader(true);
		}
		return leader;
	}
	
	public void cleanHorde()
	{
		if(horde != null)
		{
			for(int i=0;i<horde.size();i++)
			{
				if((AdvancedMob) this.worldObj.getEntityByID(horde.get(i).entityId) == null)
				{
					horde.remove(i);
				}
			}
		}
	}
	
    public boolean isLeader()
    {
    	return leader;
    }
    
    public void setLeader(boolean leader)
    {
    	this.leader = leader;
    	this.tasks.removeTask(new EntityAIMoveTowardsLeader(this, this.moveSpeed, 128));
    	//System.out.println("Removed Task");
    }
	
    protected void addToHorde()
    {
    	if(null == horde || horde.size() == 0)
    	{
    		horde = new ArrayList<AdvancedMob>();
    		//System.out.println("Created Horde");
    	}
    	else if(leaderID == -1)
    	{
    		leaderID = this.entityId;
    		this.setLeader(true);
    		//System.out.println(this.entityId + " is the leader");
    	}
    	horde.add(this);
    	//System.out.println("Added to horde " + this.entityId + " " + this.getEntityName());
    }
}
